<?php
/* 订单列表
* @Author: Jerry-chen
* @Date:   2014-07-21 15:23:20
* @Last Modified time: 2014-07-23 17:24:38
* @Last Modified by: Jerry-chen<uranusbill@163.com>
* @ Copyright (c) 2014 http://www.cooldreamer.com All rightsreserved.
*/

class OrderControl extends CommonControl
{
	
	function index()
	{
		//查出所有订单
		$orderdata = K('Order')->get_all();
		// p($orderdata);
		$count = 0;
		foreach ($orderdata as $k => $v) {
			$orderlist = K('Orderlist')->get_all(array('orderid'=>$v['orderid']),array('timeid','goodsid','price','number','recount'));
			//格式化下单时间
			$orderdata[$k]['ordertime'] = date('Y-m-d h:i:s',$v['ordertime']);
			// p($orderlist);
			$orderdata[$k]['orderlist'] = $orderlist;
				foreach ($orderlist as $key => $value) {
					// 根据时间ID，查出时间
				$timename = K('Goodstime')->get_one(array('timeid'=>$value['timeid']),array('time'));
				$orderdata[$k]['orderlist'][$key]['time'] = $timename['time'];
				//根据货品ID查出货物名
				$goodsname = K('Goods')->get_one(array('goodsid'=>$value['goodsid']),array('goodsname'));
				$orderdata[$k]['orderlist'][$key]['goodsname'] = $goodsname['goodsname'];
				//价格
				$orderdata[$k]['orderlist'][$key]['price'] = $value['price'];
				$orderdata[$k]['orderlist'][$key]['number'] = $value['number'];
				$orderdata[$k]['orderlist'][$key]['recount'] = $value['recount'];
				$count += $value['recount'];
				//订单合计
				}
		}		
		// p($orderdata);
		$this->assign('orderdata',$orderdata);
		$this->assign('count',$count);
		// die;
		$this->display();
	}
	/**
	 * [email_goods 邮件发货]
	 * @return [type] [description]
	 */
	public function email_goods()
	{
		// p(Q('get.'));
		
		//用户名
		$orderdata = K('Order')->get_one(array('orderid'=>Q('get.orderid'))); 
		//订单号
		//取货方式
		// p($orderdata);
		// die;
		$state = Mail::send(
		     "{$orderdata['email']}",
		     " 大麦票务 ",
		      "欢迎您在大麦网购票",
		      " 尊敬的{$orderdata['name']}：您的订单 {$orderdata['ordernumber']}已提交成功，您可以凭此32领票密码{$orderdata['tickpassword']}领取取票，请妥善保管您的领取密码"
		     
 		 );  
		// p($orderdata);die;
		// p($state);
		//如果发送成功
 		  if($state){
	 		  	$data =array(
				'stadus'=>2,
				);
				$result = K('Order')->edit(array('orderid'=>Q('get.orderid')),$data);
				$this->success('成功发货','');
	 		  
	 		}else{
	 			$this->success('数据连接错误','');
	 		}
		}
		/**
		 * [handle 设置订单状态]
		 * @return [type] [description]
		 */
	public function handle()
	{
		//捃行前面的方法，获得相关数据
		// $this->index();
		$this->display();
	}
}